<?php

namespace Modules\Shop\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Shop\Entities\Category;
use Modules\Shop\Http\Requests\CategoryRequest;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {

        //获取所有分类名称
        //$categorys = Category::get();
        //把关联的表用属性调用就可以使用关联方法
//        $cate = Category::find(1);
//        $cate->classification->getAll();
        //dd($categorys);
        //获取所有数据（树状结构）
        $categorys = Category::getTreeCategory();
        //dd($categorys);
        //加载模板
        return view('shop::category.index',compact('categorys'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('shop::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(CategoryRequest $request)
    {
        //获取post数据
        $post = $request->all();
        //dd($post);
        //写入数据库
        //这里会报错，需在模型里定义protected $guarded = [];
        Category::create($post);
        //设置闪存信息
        session()->flash('success','添加成功');
        //跳转
        $categorys = Category::getTreeCategory();
        return redirect()->route('category.index',compact('categorys'));


    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('shop::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($id)
    {
        //需要编辑的数据的旧数据；
        $editid = Category::find($id);
        //dd($editid);
        //获取所有的分类数据
        $categorys = Category::getTreeCategory($editid);
        //dd($categorys );

        return view('shop::category.edit',compact('categorys','editid'));
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(CategoryRequest $request,$id)
    {
        //需要编辑的数据的id；
        $editid = Category::find($id);
        //获取post数据
        $post = $request->all();
        //验证
        $editid->cname = $post['cname'];
        $editid->pid = $post['pid'];
        //存储数据库
        $editid->save();
        //设置闪存信息
        session()->flash('success','编辑成功');
        //跳转
        return redirect()->route('category.index');



    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        //查找要删除的id
        $editid = Category::find($id);
        //判断有子集的话不能删除
            if (Category::hasChild($editid)){
                //设置闪存信息
                session()->flash('danger','请先删除当前的子集，再来删除该分类');
                return back();
            }

        //执行删除
        Category::destroy($id);
        session()->flash('success','删除成功');
        return redirect()->route('category.index');


    }
}
